package com.bionic.banking.dao.classes;

import com.bionic.banking.core.entity.BankTransaction;
import com.bionic.banking.dao.IBankTransactionDao;
import com.bionic.banking.dao.connection.DBQueryManager;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class BankTransactionDaoImpl implements IBankTransactionDao {
    
    protected final String SQL_SELECT = "SELECT * FROM " + getTableName() + " ";
    protected final String SQL_INSERT = "INSERT INTO " + getTableName()
            + "(tx_type_id, description, account_id, target_account_id, balance, amount,currency_id, time_stamp)"
            + " VALUES(?,?,?,?,?,?,?,?)";
    protected final String SQL_UPDATE = "UPDATE " + getTableName()
            + " SET description = ?, amount = ?,currency_id = ?";
    protected final String SQL_DELETE = " DELETE FROM " + getTableName();
    protected final String SQL_WHERE = " WHERE " + getKeyColumn() + "=?";
    protected static final int COLUMN_ID = 1;
    protected static final int COLUMN_TYPE = 2;
    protected static final int COLUMN_DESCRIPTION = 3;
    protected static final int COLUMN_AMOUNT = 4;
    protected static final int COLUMN_BALANCE = 5;
    protected static final int COLUMN_TIMESTAMP = 6;
    protected static final int COLUMN_CURRENCY = 7;
    protected static final int COLUMN_ACCOUNTID = 8;
    protected static final int COLUMN_TARGETACCOUNTID = 9;
    
    protected int maxRows;

    @Override
    public String getTableName() {
        return "BANK_TRANSACTION";
    }

    @Override
    public String getKeyColumn() {
        return "TX_ID";
    }

    @Override
    public DBQueryManager<BankTransaction> getDBManager() {
        return new DBQueryManager<>(this);
    }

    @Override
    public BankTransaction populateDto(ResultSet rs) throws SQLException {
        //TODO:
        BankTransaction dto = new BankTransaction();
        dto.setTxId(rs.getInt(COLUMN_ID));
        dto.setTxTypeId(rs.getInt(COLUMN_TYPE));
        dto.setDescription(rs.getString(COLUMN_DESCRIPTION));
        dto.setAmount(BigInteger.valueOf(rs.getLong(COLUMN_AMOUNT)));
        dto.setBalance(new BigInteger(rs.getString(COLUMN_BALANCE)));
        dto.setTimeStamp(rs.getTimestamp(COLUMN_TIMESTAMP));
        dto.setCurrencyId(rs.getInt(COLUMN_CURRENCY));
        dto.setAccountId(rs.getInt(COLUMN_ACCOUNTID));
        dto.setTargetAccountId(rs.getInt(COLUMN_TARGETACCOUNTID));
        return dto;
    }

    @Override
    public Integer create(BankTransaction transaction) throws SQLException {
        Object[] params = new Object[]{
            transaction.getTxTypeId(),
            transaction.getDescription(),
            transaction.getAccountId(),
            transaction.getTargetAccountId(),
            transaction.getBalance(),
            transaction.getAmount(),
            transaction.getCurrencyId(),
            transaction.getTimeStamp()
        };
        Integer newTransactionId = getDBManager().executeCreateQuery(SQL_INSERT, params);
        transaction.setTxId(newTransactionId); //update with new generated PK in DB 
        return newTransactionId;
    }

    @Override
    public boolean update(BankTransaction transaction) throws SQLException {
        final String query = SQL_UPDATE + SQL_WHERE;
        Object[] params = new Object[]{
            //transaction.getTxTypeId(), 
            transaction.getDescription(),
            transaction.getAmount(),
            transaction.getCurrencyId(),
            //transaction.getTimeStamp(),
            transaction.getTxId()
        };
        return getDBManager().executeSaveQuery(query, params);
    }

    @Override
    public void remove(int id) throws SQLException {
        final String query = SQL_DELETE + SQL_WHERE;
        getDBManager().executeRemoveQuery(query, id);
    }

    @Override
    public BankTransaction findById(int id) throws SQLException {
        String query = SQL_SELECT + SQL_WHERE;
        Object[] params = new Object[]{new Integer(id)};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override//TODO:
    public List<BankTransaction> findByAccountId(int accoutId) throws SQLException {
        String query = SQL_SELECT + " WHERE ACCOUNT_ID = ?";
        Object[] params = new Object[]{accoutId};
        return getDBManager().executeSelectQuery(query, params);
    }

    @Override
    public List<BankTransaction> findAll() throws SQLException {
        String query = SQL_SELECT;
        return getDBManager().executeSelectQuery(query, null);
    }

    @Override
    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    @Override
    public int getMaxRows() {
        return maxRows;
    }
}